<template>
  <div class="app-container">
    <el-row class="row bg-white">
      <el-row>
        <el-button type="primary" class="green-btn m-bt-20" size="small" v-if="btnTypeAdd" style="width: 120px" @click="handleCreate">添加广告类型</el-button>
      </el-row>
      <el-table :data="list"  element-loading-text="Loading" border fit highlight-current-row>
        <el-table-column label='名称' align="center">
          <template slot-scope="scope">
            {{scope.row.name}}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center">
          <template slot-scope="scope">
            {{scope.row.created_at}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="230" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" class="green-btn-min" v-if="btnTypeDelete" @click="handleDelete(scope.row.id)" >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
      <el-dialog title="新建广告类型" :visible.sync="dialogFormVisible"  width="25%" center>
        <el-form center ref="adTypeForm" :model="adTypeForm" :rules="adTypeRules" label-position="left" label-width="120px">
          <el-input size="small" style="width: 300px;" type="hidden" v-model="adTypeForm.id"></el-input>
          <el-form-item label="广告类型名称"  prop="name">
            <el-input size="small"  style="width: 300px;" v-model="adTypeForm.name" placeholder="请输入广告版位" ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button size="small" class="green-btn" type="primary"  @click="createData">创建</el-button>
        <el-button size="small" class="w-130" @click="dialogFormVisible = false">取消</el-button>
      </span>
      </el-dialog>
    </el-row>
  </div>
</template>

<script>
  import { getAdTypePagingList, adTypeSave, adTypeDelete } from '@/api/assistive'
  import { in_array } from '@/utils/index'

  export default {
    name: 'recharge',
    data() {
      return {
        list: [],
        total: null,
        listQuery: {
          page: 1,
          pageSize: 10
        },
        dialogFormVisible: false,
        dialogStatus: '',
        textMap: {
          update: '编辑广告类型',
          create: '新建广告类型'
        },
        adTypeForm: {
          id: '',
          name: ''
        },
        adTypeRules: {
          name: [{ required: true, message: '请输入广告类型(2-5字符)', trigger: 'blur' },
            { min: 2, max: 5, message: '请输入广告类型(2-5字符)', trigger: 'blur' }]
        },
        btnTypeAdd: false,
        btnTypeDelete: false
      }
    },
    created() {
      this.getButtonStatus()
      this.getList()
    },
    methods: {
      handleCreate() {
        this.dialogFormVisible = true
      },
      handleDelete(id) {
        this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const data = {}
          data.id = id
          adTypeDelete(data).then(response => {
            if (response.code === 1) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.getList()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      handleSizeChange(val) {
        this.listQuery.pageSize = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getList()
      },
      getList() {
        getAdTypePagingList(this.listQuery).then(response => {
          if (response.code === 1) {
            this.list = response.data.list
            this.total = response.data.total
            setTimeout(() => {
              this.listLoading = false
            }, 1.5 * 1000)
          }
        })
      },
      createData() {
        this.$refs.adTypeForm.validate(valid => {
          if (valid) {
            adTypeSave(this.adTypeForm).then(response => {
              if (response.code === 1) {
                this.$message({
                  title: '成功',
                  message: '创建成功！',
                  type: 'success',
                  duration: 3000
                })
                this.dialogFormVisible = false
                this.getList()
                this.$refs.adTypeForm.resetFields()
              }
            })
          }
        })
      },
      getButtonStatus() {
        const btnList = this.$store.getters.buttons
        if (in_array('typeAdd', btnList)) {
          this.btnTypeAdd = true
        }
        if (in_array('typeDelete', btnList)) {
          this.btnTypeDelete = true
        }
      }
    }
  }
</script>

